package com.example.zxc;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Intent intent;
    EditText etname;
    EditText etpassword;
    Button dl;
    Button zc;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etname = (EditText) findViewById(R.id.editText);
        dl = (Button) findViewById(R.id.button);

        init();

    }
    private void init() {
        etname = (EditText) findViewById(R.id.editText);
        etpassword= (EditText) findViewById(R.id.editText3);
        dl = (Button) findViewById(R.id.button);
        zc= (Button) findViewById(R.id.button2);
        dl.setOnClickListener(this);
        zc.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {

        String name=etname.getText().toString().trim();
        String password=etpassword.getText().toString().trim();

        switch (view.getId()) {
            case R.id.button:
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                String pwd=denglu.getUserInfo(this,name);
                if(pwd==null){
                    Toast.makeText(this,"该用户不存在",Toast.LENGTH_SHORT).show();
                }else if(password.equals(pwd)){
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(MainActivity.this,shouye.class);
                    intent.putExtra("name",etname.getText().toString().trim());
                    startActivity(intent);
                    MainActivity.this.finish();
                }else{
                    Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show();
                }
                ((EditText) findViewById(R.id.editText)).setText("");
                ((EditText) findViewById(R.id.editText3)).setText("");
                break;
            case R.id.button2:
                boolean isSaveSuccess= denglu.saveUserInfo(this,name,password);
                if(isSaveSuccess){
                    Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"注册失败",Toast.LENGTH_SHORT).show();
                }
                ((EditText) findViewById(R.id.editText)).setText("");
                ((EditText) findViewById(R.id.editText3)).setText("");
        }
    }

}


